/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package chameleon.model.NuGatManager.Strategy;

import chameleon.gameSolver.NuGat.NuGatInterface;
import chameleon.model.NuGatStructure.bdd.Bdd;
import chameleon.model.NuGatStructure.enc.BddEnc;
import chameleon.model.NuGatStructure.bdd.DdManager;
import chameleon.model.NuGatStructure.game.GamePlayer;
import chameleon.model.NuGatStructure.utils.Node;
import chameleon.model.NuGatStructure.prop.PropGame;
import chameleon.view.OutputManager;

/**
 * This class is a wrapper to a PreImage.
 *
 * @author simon
 */
public class PreImage {

    private PropGame target_; //PropGame_ptr
    private Node reachStateList_; //node_ptr (with bdd_ptr)
    private NuGatInterface nugat_;
    private boolean isTargetReached_;
    private boolean isFixedpointReached_;
    private GamePlayer player_;
    private GamePlayer opponent_;
    private char quantifiers_;
    private Bdd originalTarget_;
    private Bdd allReachStates_;

    //********************* CONSTRUCTOR *************************************//
    
    public PreImage(PropGame gameWc, Node preImage, NuGatInterface nugat,
            boolean isReachedTarget, boolean isFixedpointReached,
            GamePlayer player, GamePlayer opponent, char quantifiers,
            Bdd originalTarget, Bdd allReachStates ){
        target_ = gameWc;
        reachStateList_ = preImage;
        nugat_ = nugat;
        isTargetReached_ = isReachedTarget;
        player_ = player;
        quantifiers_ = quantifiers;
        originalTarget_ = originalTarget;
        allReachStates_ = allReachStates;
        isFixedpointReached_ = isFixedpointReached;
        opponent_ = opponent;
    }

    //********************** DESTRUCTOR *************************************//
    
    public void destroy(DdManager ddManager){
        ddManager.bddFree(originalTarget_);
        ddManager.bddFree(allReachStates_);
    }
    
    public Node getReachStateList(){
        return reachStateList_;
    }

    public PropGame getProp(){
        return target_;
    }
    
    public String getTargetString(){
        return target_.toString();
    }

/*    public void setPreImage(Node preImage){
        reachStateList_ = preImage;
    }
*/
    public boolean isTargetReached(){
        return isTargetReached_;
    }

    public boolean isAvoidedTarget(){
        return !isTargetReached_;
    }
    
    public boolean isFixedpointReached(){
        return isFixedpointReached_;
    }
    
    public GamePlayer getPlayer(){
        return player_;
    }
    
    public GamePlayer getOpponent(){
        return opponent_;
    }

    public char getQuantifiers(){
        return quantifiers_;
    }
    
    public Bdd getOriginalTarget(){
        return originalTarget_;
    }
    
    public Bdd getAllReachStates(){
        return allReachStates_;
    }

    /**
     * Print all the states than constitute the winning region.
     */
    
    public void printReachStateList(){
        int i = 1;
        OutputManager output = new OutputManager();
        output.displayString("The result is print on files reachSateListN");
        for (Node n = reachStateList_; n.notNull(); n = n.cdr()){
            n.printNode("reachStateList"+i);
        }
    }
    
    public int getStatus(){
        return ( nugat_.PropGame_ReachTarget == target_.getType()
      || nugat_.PropGame_ReachDeadlock == target_.getType()
    )
    ? (isTargetReached_ ? nugat_.GAME_REALIZABLE : nugat_.GAME_UNREALIZABLE)
    : (isTargetReached_ ? nugat_.GAME_UNREALIZABLE : nugat_.GAME_REALIZABLE);
    }
}
